package com.glsc.ngateway.platform.service.itflow.flowstrategy.equipmentModify.taskPostHandler;

import com.alibaba.excel.util.CollectionUtils;
import com.glsc.ngateway.common.api.flowable.dto.task.doTaskParam.equipment.EquipmentPassParamDto;
import com.glsc.ngateway.common.api.flowable.dto.task.doTaskParam.equipment.EquipmentRejectParamDto;
import com.glsc.ngateway.common.api.flowable.dto.task.formData.equipment.EquipmentModifyTaskFormDataDto;
import com.glsc.ngateway.common.api.flowable.dto.task.formData.equipment.domainDto.FlowFormEquipmentModifyDto;
import com.glsc.ngateway.platform.service.RoleService;
import com.glsc.ngateway.platform.service.itflow.flowstrategy.AbstractHandlerHelper;
import com.glsc.ngateway.common.base.enums.itflow.ITFlowTaskEnum;
import com.glsc.ngateway.common.base.enums.itflow.ITFlowTaskRoleEnum;
import io.jsonwebtoken.lang.Collections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.time.LocalDate;
import java.util.List;

/**
 * 设备管理员库存确认
 *
 * @author xuchang
 */
@Component
public class EMStep3DeviceAdminTaskPostHandlerAudit implements IEquipmentModifyTaskPostHandlerAudit {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private RoleService roleService;

    @Autowired
    private EMStepHandlerHelper handlerHelper;

    /**
     * 驳回后置处理方法
     *
     * @param passParam
     * @param operator
     */
    @Override
    public void rejectAfterHandle(@NotNull EquipmentRejectParamDto passParam, @NotNull String operator) {
        handlerHelper.afterHandle(passParam, operator);
    }

    @Override
    public void passAfterHandle(@NotNull EquipmentPassParamDto<FlowFormEquipmentModifyDto, EquipmentModifyTaskFormDataDto> param, String operator) {
        //抄送给信息安全岗
        List<String> accountList = roleService.getUsersByRole(ITFlowTaskRoleEnum.ROLE_INFO_SECURITY.getRoleName());
        if (Collections.isEmpty(accountList)) {
            logger.info("设备变更流程-设备管理员库存确认-抄送:无抄送人");
        } else {
            EquipmentModifyTaskFormDataDto data = param.getFlowableFormData();
            LocalDate flowDesiredDate = data.getFormMainData().getFlowDesiredDate();
            handlerHelper.copyPorcess(param.getProcessId(), operator, accountList
                    , String.format("【抄送】-【设备变更流程】-【%s】", data.getProcessTitle())
                    , AbstractHandlerHelper.GATEWAY_IT_FLOW_URL + "equipmentFlowAudit?flowProcessId=" + param.getProcessId()
                    , String.format("申请人：" + data.getFormMainData().getFlowCreateOperatorName() +
                            "<br/>  变更服务器：" +
                            (CollectionUtils.isEmpty(data.getFormServerDataList()) ? "0" : data.getFormServerDataList().size()) + "台" +
                            "<br/>  变更虚拟机：" +
                            (CollectionUtils.isEmpty(data.getFormVirtualDeviceDataList()) ? "0" : data.getFormVirtualDeviceDataList().size()) + "台" +
                            "<br/>  变更云平台：" +
                            (CollectionUtils.isEmpty(data.getFormCloudDeviceDataList()) ? "0" : data.getFormCloudDeviceDataList().size()) + "台" +
                            "<br/>  希望使用时间：" + flowDesiredDate.getYear() + "年" + flowDesiredDate.getMonthValue() + "月" + flowDesiredDate.getDayOfMonth() + "日" +
                            "<br/>  请知悉。可至国联技术服务平台内查阅相关事宜。"));
        }
    }

    @Override
    public String getTaskDefinitionKey() {
        return ITFlowTaskEnum.MODIFY_FLOW_DEVICE_ADMIN_TASK.getTaskKey();
    }
}
